// The module 'vscode' contains the VS Code extensibility API
// Import the module and reference it with the alias vscode in your code below
import * as vscode from 'vscode';

// This method is called when your extension is activated
// Your extension is activated the very first time the command is executed
export function activate(context: vscode.ExtensionContext) {

	// Use the console to output diagnostic information (console.log) and errors (console.error)
	// This line of code will only be executed once when your extension is activated
	console.log('Congratulations, your extension "filestructureforvue" is now active!');

	// The command has been defined in the package.json file
	// Now provide the implementation of the command with registerCommand
	// The commandId parameter must match the command field in package.json
	const disposable = vscode.commands.registerCommand('filestructureforvue.createvue', async () => {
		// The code you place here will be executed every time your command is executed
		// Display a message box to the user
		const fileName = await vscode.window.showInputBox({
			prompt: 'Enter the name of the Vue file (without .vue extension)',
			placeHolder: 'e.g. MyComponent'
		});

		if (!fileName) {
			vscode.window.showErrorMessage('Filename is required.');
			return;
		}

		// Prompt the user to select a folder
		const folderUri = await vscode.window.showOpenDialog({
			canSelectFiles: false,
			canSelectFolders: true,
			canSelectMany: false,
			openLabel: 'Select Folder'
		});

		if (!folderUri || folderUri.length === 0) {
			vscode.window.showErrorMessage('Folder selection is required.');
			return;
		}

		const folderPath = folderUri[0].fsPath;
		const filePath = vscode.Uri.file(`${folderPath}/${fileName}.vue`);


		// Define the basic structure of a Vue file
		const vueFileContent = `
<template>
  <div class="${fileName}">
    <!-- Add your template code here -->
  </div>
</template>

<script setup>

</script>

<style scoped>

</style>
`.trim();

		// Write the content to the new Vue file
		try {
			await vscode.workspace.fs.writeFile(filePath, Buffer.from(vueFileContent, 'utf8'));
			vscode.window.showInformationMessage(`Vue file "${fileName}.vue" created successfully.`);
		} catch (error) {
			vscode.window.showErrorMessage(`Failed to create Vue file: ${error}`);
		}
	});

	context.subscriptions.push(disposable);
}

// This method is called when your extension is deactivated
export function deactivate() { }
